﻿using System;
using Slf.Resolvers;

namespace Slf.Samples.NamesLoggersAtRuntime
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.ForegroundColor = ConsoleColor.Green;
      Console.Out.WriteLine("We plug in a DelegateResolver that invokes a delegate we specify at runtime to get logger factories.\n\n");
      Console.ResetColor();

      //use a delegate resolver to simply return the logger for a given request
      //(under the hood, this resolver uses a DelegateFactory)
      DelegateResolver resolver = new DelegateResolver(GetLogger);
      
      //plug in the resolver
      LoggerService.FactoryResolver = resolver;

      CreateLogEntries();

      Console.ReadLine();
    }

    private static void CreateLogEntries()
    {
      ILogger testLogger = LoggerService.GetLogger("Test");
      ILogger productionLogger = LoggerService.GetLogger("Production");
      ILogger loggerWithUnknownName = LoggerService.GetLogger("Some Unknown Logger Name");
      ILogger defaultLogger = LoggerService.GetLogger();

      testLogger.Info("Logging to test logger.");
      productionLogger.Info("Logging to production logger.");
      loggerWithUnknownName.Info("Logging to logger that has an unknown name.");
      defaultLogger.Info("Logging to default logger.");
    }


    /// <summary>
    /// This is a factory method that is supposed to return a specific logger
    /// depending on the submitted logger name.
    /// </summary>
    private static ILogger GetLogger(string loggername)
    {
      //in a real-life scenario, you would rather cache logger instances,
      //but keep things simple

      switch (loggername)
      {
        case "Test":
          return new ConsoleLogger(loggername);
        case "Production":
          return new ColoredConsoleLogger(loggername);
        default:
          //fall back to this logger if the logger name is unknown
          return new DelegateLogger(li => Console.WriteLine("[DEFAULT LOGGER] " + li.Message));
      }


    }
  }
}